/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.dao.hibernate;

import com.minierp.bean.UserBean;
import com.minierp.dao.SetuBillDao;
import com.minierp.dao.hibernate.BaseDaoImpl;
import com.minierp.helper.EncCommonHelper;
import com.minierp.model.SetuBill;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

public class SetuBillDaoImpl extends BaseDaoImpl implements SetuBillDao {
	public SetuBill get(String billCode) {
		String sql = "from com.minierp.model.SetuBill as obj where obj.billCode = :billCode and obj.enab=\'Y\' order by obj.id";
		String[] paramNames = new String[]{"billCode"};
		Object[] values = new Object[]{billCode};
		List list = this.getHibernateTemplate().findByNamedParam(sql,
				paramNames, values);
		return list.isEmpty() ? null : (SetuBill) list.get(0);
	}

	public int examine(final long id, final long examId, final String examName) {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "update com.minierp.model.SetuBill set exam=\'Y\' ,examId="
								+ examId
								+ " ,examName=:examName"
								+ " where id=" + id;
						return new Integer(session.createQuery(hql)
								.setString("examName", examName)
								.executeUpdate());
					}
				});
		return ((Integer) obj).intValue();
	}

	public List getList(String searchKey, long tsId, long estManagerId,
			UserBean ub, Date allowStart, String orderBy, int startIndex,
			int num) throws Exception {
		String str = "";
		if (searchKey != null) {
			searchKey = searchKey.trim();
		}

		if (searchKey != null && !searchKey.equals("")) {
			str = " and ((b.billCode like :billCode)  or (e.estName like :estName) or (e.nameSpell like :nameSpell) or (bb.billCode like :bbCode) or (bb.content like :content))";
		}

		if (tsId != 0L) {
			str = str + " and b.tsId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tsId", ub, true);
		}

		if (estManagerId != 0L) {
			str = str + " and e.managerId=" + estManagerId;
		}

		if (allowStart != null) {
			str = str + " and b.billDate>=:allowStart";
		}

		String sql = "select b,e.estName,bb.billCode,bb.content from com.minierp.model.SetuBill b,com.minierp.model.BusinessBill bb,com.minierp.model.Establishment e where b.bbId=bb.id and bb.clientId=e.id"
				+ str + " and b.enab=\'Y\'";
		if (orderBy != null && orderBy.equals("BILL_DATE_ASC")) {
			sql = sql + " order by b.billDate asc";
		} else {
			sql = sql + " order by b.billDate desc";
		}

		Session sess = null;

		List arg15;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			if (searchKey != null && !searchKey.equals("")) {
				q.setString("billCode", "%" + searchKey + "%");
				q.setString("estName", "%" + searchKey + "%");
				q.setString("nameSpell", "%" + searchKey + "%");
				q.setString("bbCode", "%" + searchKey + "%");
				q.setString("content", "%" + searchKey + "%");
			}

			if (allowStart != null) {
				q.setDate("allowStart", allowStart);
			}

			q.setFirstResult(startIndex);
			q.setMaxResults(num);
			arg15 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg15;
	}

	public int getListCount(String searchKey, long tsId, long estManagerId,
			UserBean ub, Date allowStart) throws Exception {
		String str = "";
		if (searchKey != null) {
			searchKey = searchKey.trim();
		}

		if (searchKey != null && !searchKey.equals("")) {
			str = " and ((b.billCode like :billCode)  or (e.estName like :estName) or (e.nameSpell like :nameSpell) or (bb.billCode like :bbCode) or (bb.content like :content))";
		}

		if (tsId != 0L) {
			str = str + " and b.tsId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tsId", ub, true);
		}

		if (estManagerId != 0L) {
			str = str + " and e.managerId=" + estManagerId;
		}

		if (allowStart != null) {
			str = str + " and b.billDate>=:allowStart";
		}

		String sql = "select count(b.id) from com.minierp.model.SetuBill b,com.minierp.model.BusinessBill bb,com.minierp.model.Establishment e where b.bbId=bb.id and bb.clientId=e.id"
				+ str + " and b.enab=\'Y\'";
		Session sess = null;

		int arg13;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			if (searchKey != null && !searchKey.equals("")) {
				q.setString("billCode", "%" + searchKey + "%");
				q.setString("estName", "%" + searchKey + "%");
				q.setString("nameSpell", "%" + searchKey + "%");
				q.setString("bbCode", "%" + searchKey + "%");
				q.setString("content", "%" + searchKey + "%");
			}

			if (allowStart != null) {
				q.setDate("allowStart", allowStart);
			}

			List list = q.list();
			if (list.isEmpty()) {
				return 0;
			}

			arg13 = Integer.parseInt(list.get(0).toString());
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg13;
	}

	public List getList(String searchKey, boolean examined, long tsId,
			long estManagerId, UserBean ub, Date allowStart, String orderBy,
			int startIndex, int num) throws Exception {
		String str = "";
		if (searchKey != null) {
			searchKey = searchKey.trim();
		}

		if (searchKey != null && !searchKey.equals("")) {
			str = " and ((b.billCode like :billCode)  or (e.estName like :estName) or (e.nameSpell like :nameSpell) or (bb.billCode like :bbCode) or (bb.content like :content))";
		}

		if (tsId != 0L) {
			str = str + " and b.tsId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tsId", ub, true);
		}

		if (estManagerId != 0L) {
			str = str + " and e.managerId=" + estManagerId;
		}

		if (allowStart != null) {
			str = str + " and b.billDate>=:allowStart";
		}

		String examineFlag = "N";
		if (examined) {
			examineFlag = "Y";
		}

		String sql = "select b,e.estName,bb.billCode,bb.content from com.minierp.model.SetuBill b,com.minierp.model.BusinessBill bb,com.minierp.model.Establishment e where b.bbId=bb.id and bb.clientId=e.id"
				+ str
				+ " and b.exam=\'"
				+ examineFlag
				+ "\'"
				+ " and b.enab=\'Y\'";
		if (orderBy != null && orderBy.equals("BILL_DATE")) {
			sql = sql + " order by b.billDate";
		} else {
			sql = sql + " order by b.id desc";
		}

		Session sess = null;

		List arg17;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			if (searchKey != null && !searchKey.equals("")) {
				q.setString("billCode", "%" + searchKey + "%");
				q.setString("estName", "%" + searchKey + "%");
				q.setString("nameSpell", "%" + searchKey + "%");
				q.setString("bbCode", "%" + searchKey + "%");
				q.setString("content", "%" + searchKey + "%");
			}

			if (allowStart != null) {
				q.setDate("allowStart", allowStart);
			}

			q.setFirstResult(startIndex);
			q.setMaxResults(num);
			arg17 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg17;
	}

	public int getListCount(String searchKey, boolean examined, long tsId,
			long estManagerId, UserBean ub, Date allowStart) throws Exception {
		String str = "";
		if (searchKey != null) {
			searchKey = searchKey.trim();
		}

		if (searchKey != null && !searchKey.equals("")) {
			str = " and ((b.billCode like :billCode)  or (e.estName like :estName) or (e.nameSpell like :nameSpell) or (bb.billCode like :bbCode) or (bb.content like :content))";
		}

		if (tsId != 0L) {
			str = str + " and b.tsId=" + tsId;
		} else {
			str = str
					+ EncCommonHelper.createTallySetSQLStr("b.tsId", ub, true);
		}

		if (estManagerId != 0L) {
			str = str + " and e.managerId=" + estManagerId;
		}

		if (allowStart != null) {
			str = str + " and b.billDate>=:allowStart";
		}

		String examineFlag = "N";
		if (examined) {
			examineFlag = "Y";
		}

		String sql = "select count(b.id) from com.minierp.model.SetuBill b,com.minierp.model.BusinessBill bb,com.minierp.model.Establishment e where b.bbId=bb.id and bb.clientId=e.id"
				+ str
				+ " and b.exam=\'"
				+ examineFlag
				+ "\'"
				+ " and b.enab=\'Y\'";
		Session sess = null;

		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			if (searchKey != null && !searchKey.equals("")) {
				q.setString("billCode", "%" + searchKey + "%");
				q.setString("estName", "%" + searchKey + "%");
				q.setString("nameSpell", "%" + searchKey + "%");
				q.setString("bbCode", "%" + searchKey + "%");
				q.setString("content", "%" + searchKey + "%");
			}

			if (allowStart != null) {
				q.setDate("allowStart", allowStart);
			}

			List list = q.list();
			if (!list.isEmpty()) {
				int arg15 = Integer.parseInt(list.get(0).toString());
				return arg15;
			}
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return 0;
	}

	public Object[] getStatusValues(long id) {
		String sql = "select b.updaTime,b.enab,b.exam,b.setuIdStr from com.minierp.model.SetuBill b where b.id="
				+ id;
		List list = this.getHibernateTemplate().find(sql);
		return list.size() > 0 ? (Object[]) list.get(0) : null;
	}

	public List<?> getListByBusinessBill(long businessBillId) {
		String sql = "from com.minierp.model.SetuBill b where b.enab=\'Y\' and b.bbId="
				+ businessBillId + " order by b.billDate";
		return this.getHibernateTemplate().find(sql);
	}

	public int clearSetuDetail(final long billId) {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "delete com.minierp.model.SetuDetail where billId = :billId";
						return new Integer(session.createQuery(hql)
								.setLong("billId", billId).executeUpdate());
					}
				});
		return ((Integer) obj).intValue();
	}
}